package com.pozu.new_retail.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pozu.new_retail.dao.vo.AttributeVo;
import com.pozu.new_retail.dao.vo.GoodsExtVo;
import com.pozu.new_retail.dao.mapper.GoodsExtMapper;
import com.pozu.new_retail.dao.model.GoodsExt;
import com.pozu.new_retail.service.GoodsExtService;

@Service("goodsExtService")
public class GoodsExtServiceImpl implements GoodsExtService {

	@Autowired
	private GoodsExtMapper goodsExtMapper;
	
	@Override
	public List<GoodsExtVo> queryByGoodsId(int goodsId) {
		List<GoodsExtVo> vos = new ArrayList<GoodsExtVo>(); 
		List<GoodsExt> list = goodsExtMapper.queryByGoodsId(goodsId);
		
		if(null != list &&list.size()>0){
			for(GoodsExt ext:list){
				if(null != ext){
					GoodsExtVo vo = new GoodsExtVo();
					vo.setImg(ext.getImg());
					vo.setGoodsExtId(ext.getGoodsExtId());
					vo.setGoodsId(ext.getGoodsId());
					vo.setNumber(ext.getNumber());
					vo.setPrice(ext.getPrice());
					
					List<AttributeVo> attributes = new ArrayList<AttributeVo>();
					
					AttributeVo attributeVo = new AttributeVo();
					
					if(null != ext.getAttr1()&& !"".equals(ext.getAttr1())&&null != ext.getValue1()&&!"".equals(ext.getValue1())){
						attributeVo.setAttrKey(ext.getAttr1());
						attributeVo.setAttrValue(ext.getValue1());
						attributes.add(attributeVo);
					}
					
					if(null != ext.getAttr2()&& !"".equals(ext.getAttr2())&&null != ext.getValue2()&&!"".equals(ext.getValue2())){
						attributeVo = new AttributeVo();
						attributeVo.setAttrKey(ext.getAttr2());
						attributeVo.setAttrValue(ext.getValue2());
						attributes.add(attributeVo);
					}
					
					if(null != ext.getAttr3()&& !"".equals(ext.getAttr3())&&null != ext.getValue3()&&!"".equals(ext.getValue3())){
						attributeVo = new AttributeVo();
						attributeVo.setAttrKey(ext.getAttr3());
						attributeVo.setAttrValue(ext.getValue3());
						attributes.add(attributeVo);
					}
					
					if(null != ext.getAttr4()&& !"".equals(ext.getAttr4())&&null != ext.getValue4()&&!"".equals(ext.getValue4())){
						attributeVo = new AttributeVo();
						attributeVo.setAttrKey(ext.getAttr4());
						attributeVo.setAttrValue(ext.getValue4());
						attributes.add(attributeVo);
					}
					vo.setAttributes(attributes);
					vos.add(vo);
				}
			}
		}
		
		return vos;
	}

	@Override
	public GoodsExt queryByGoodsExtId(int goodsExtId) {
		return goodsExtMapper.queryByGoodsExtId(goodsExtId);
	}

	@Override
	public int batchUpdate(List<GoodsExt> goodsExts) {
		return goodsExtMapper.batchUpdate(goodsExts);
	}

}
